/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mud;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

/**
 *
 * @author jlink005
 */
public class MUDServerImpl implements MUDServer {

    private final SimpleServerSocket serverSocket;
    private List<SocketHandler> socketHandlers = new LinkedList<SocketHandler>();
    private Executor threadPool = Executors.newFixedThreadPool(50);

    public MUDServerImpl(SimpleServerSocket serverSocket) {
        this.serverSocket = serverSocket;
    }

    public void start() {
        try {
            while (true) {
                final SimpleSocket socket = serverSocket.accept();
                threadPool.execute(new Runnable() {

                    public void run() {
                        for (SocketHandler socketHandler : socketHandlers) {
                            socketHandler.handle(socket);
                        }
                    }
                });
            }
        } catch (IOException e) {
        }
    }

    public void addSocketHandler(SocketHandler socketHandler) {
        socketHandlers.add(socketHandler);
    }
}
